// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';    // 默认主题/
import  '../static/main.css'
import {Loading, Message} from 'element-ui'
Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$http = axios
axios.defaults.baseURL = '/hdrs'
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: {App}
})

// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    let user = JSON.parse(sessionStorage.getItem('user'))
    console.log(user)
    if (to.path === '/login') {
      next()
    } else if (user === undefined || user === null) {
      console.log('to login.......')
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else {
      next()
    }
  } else {
    next()
  }

  /*if (user === undefined || user === null || from.path === '/login') {  // 判断该路由是否需要登录权限
   next({
   path: '/login',
   query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
   })
   } else {
   next();
   }*/
  next()
})

// http响应拦截器
axios.interceptors.response.use(data => {// 响应成功关闭loading
  if (data.data.flag === 2) {
    sessionStorage.removeItem('user')
    router.push({path: '/login'})
    return null
  }
  return data
}, error => {
  Message.error({
    message: '加载失败'
  })
  return Promise.reject(error)
})

